package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.CustomException;
import com.heima.feign.wemedia.IWemediaClient;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.ApUserRealnameService;
import java.util.Date;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Slf4j
@Service
@Transactional
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {

    @Autowired
    private IWemediaClient wemediaClient;

    @Autowired
    private ApUserMapper apUserMapper;

    /**
     * 查询用户列表
     *  查询app用户实名认证表ap_user_realname
     *  查询对应状态的用户认证信息
     * @param dto
     * @return
     */
    @Override
    public ResponseResult listByStatus(AuthDto dto) {
        //1.校验参数
        if(dto == null){
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.创建分页对象
        Page page = new Page(dto.getPage(), dto.getSize());

        //3.设置查询条件
        LambdaQueryWrapper<ApUserRealname> queryWrapper = Wrappers.<ApUserRealname>lambdaQuery().eq(dto.getStatus() != null, ApUserRealname::getStatus, dto.getStatus());

        //4.分页查询
        page = page(page,queryWrapper);

        //5. 封装结果返回
        PageResponseResult result = new PageResponseResult((int)page.getCurrent(),(int) page.getSize(),(int) page.getTotal());
        result.ok(AppHttpCodeEnum.SUCCESS.getCode(),page.getRecords(),AppHttpCodeEnum.SUCCESS.getErrorMessage());
        return result;
    }

    /**
     * 审核app用户信息
     * 审核成功创建自媒体用户
     * @param dto
     * @param status
     * @return
     */
    @Override
    public ResponseResult updateStatus(AuthDto dto, Short status) {
        //1.参数校验
        if(dto == null){
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.更新ap_user_realname中用户的状态信息
        ApUserRealname apUserRealname = new ApUserRealname();
        apUserRealname.setId(dto.getId());
        apUserRealname.setStatus(status);
        //如果是审核失败会传递理由
        if(StringUtils.isNotBlank(dto.getMsg())){
            apUserRealname.setReason(dto.getMsg());
        }
        updateById(apUserRealname);
        //3.如果通过就创建wemedia用户
        if(status.equals((short)9)){
            //查看是否有该app用户
            ApUser apUser = apUserMapper.selectById(apUserRealname.getId());
            if(apUser==null){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
            }
            //查看是否有该媒体用户
            WmUser wmUser = wemediaClient.findWmUser(apUser.getName());
            //如果没有则创建一个媒体用户
            if(wmUser == null){
                WmUser newWmUser = new WmUser();
                newWmUser.setApUserId(apUser.getId());
                BeanUtils.copyProperties(apUser,newWmUser);
                newWmUser.setCreatedTime(new Date());
                newWmUser.setStatus(9);
                //远程调用wemedia微服务创建自媒体用户
                wemediaClient.createWmUser(newWmUser);
            }
            //将用户改为自媒体人
            apUser.setFlag((short)1);
            apUserMapper.updateById(apUser);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


}
